package com.itheima.day12_duoxiancheng;

public class Ticket extends Thread {
    public Ticket(String name) {
        super(name);
    }

    private static int ticket = 100;

    @Override
    public void run() {
        while (true) {
                if (sellTicket()) {
                    break;
                } else {
                    try {
                        Ticket.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
        }
    }
    public synchronized  static boolean sellTicket() {
        if (ticket <= 0) {
            return true ;
        } else {
            ticket--;
            System.out.println(Thread.currentThread().getName() + "在卖票" + ticket);
            return false;
        }
    }
}

